package sk.hackcraft.multibox.android;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import sk.hackcraft.multibox.android.utils.Song;

import android.app.Activity;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PlayerActivity extends ListActivity
{
	public static int REQUEST_REGISTRATION = 1;
	
	private NetService netService;
	
	private LocalBroadcastManager localBroadcastManager;
	private BroadcastReceiver broadcastReceiver;
	
	private Intent netServiceIntent;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.player);
		
		Intent netServiceIntent = new Intent(this, NetService.class);

		startService(netServiceIntent);
		bindService(netServiceIntent, serviceConnection, 0);
		
		localBroadcastManager = LocalBroadcastManager.getInstance(this);
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(NetService.REQUEST_REGISTRATION);
		
		broadcastReceiver = new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context context, Intent intent)
			{
				if (intent.getAction().equals(NetService.REQUEST_REGISTRATION))
				{
					startActivityForResult(new Intent(PlayerActivity.this, RegistrationActivity.class), REQUEST_REGISTRATION);
				}
			}
		};
		
		localBroadcastManager.registerReceiver(broadcastReceiver, filter);

		final LinkedList<Song> songs = new LinkedList<Song>();
		songs.add(new Song("1", "Metallice", "Moe"));
		songs.add(new Song("2", "LP", "Eph"));
		songs.add(new Song("3", "Bieber", "Dusan"));
		songs.add(new Song("4", "Rebecca Black", "Marcel"));
		songs.add(new Song("5", "Metallice", "Joso"));
		songs.add(new Song("6", "Yabba", "Mbskot"));
		songs.add(new Song("7", "Yea", "Dusan"));
		songs.add(new Song("8", "hgfd", "Dusan"));
		songs.add(new Song("9", "gfd", "Dusan"));
		songs.add(new Song("10", "dfsa", "Dusan"));
		
		songs.get(0).setPlaying(true);
		
		final SongAdapter adapter = new SongAdapter(this, R.layout.song_item, songs);

		setListAdapter(adapter);
		
		getListView().setTextFilterEnabled(true);
		
		final Handler handler = new Handler();
		handler.post(new Runnable()
		{
			int number = 1;
			
			public void run()
			{
				adapter.removeSong();
				
				adapter.offerSong(new Song("Blackout", Integer.toString(number++), "Mbskot"));
				
				handler.postDelayed(this, 1000);
			}
		}
		);
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		
		localBroadcastManager.unregisterReceiver(broadcastReceiver);
		
		unbindService(serviceConnection);
		
		if (isFinishing())
		{
			stopService(netServiceIntent);
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == REQUEST_REGISTRATION)
		{
			if (resultCode == RegistrationActivity.SUCCESS)
			{
				
			}
			else
			{
				finish();
			}
		}
	};

	private ServiceConnection serviceConnection = new ServiceConnection()
	{
		@Override
		public void onServiceDisconnected(ComponentName name)
		{
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service)
		{
			netService = ((NetService.NetServiceBinder)service).getService();
		}
	};
	
	private static class SongAdapter extends ArrayAdapter<Song>
	{
		Context context;
		int layoutResourceId;
		List<Song> songs;
	    
		public SongAdapter(Context context, int layoutResourceId, List<Song> songs)
		{
			super(context, layoutResourceId, songs);
			
			this.context = context;
			this.layoutResourceId = layoutResourceId;
			this.songs = songs;
		}
		
		public void offerSong(Song song)
		{
			songs.add(song);
			notifyDataSetChanged();
		}
		
		public void removeSong()
		{
			songs.remove(0);
			songs.get(0).setPlaying(true);
			notifyDataSetChanged();
		}
	    
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View row = convertView;
			SongHolder holder = null;
			
			if (row == null)
			{
				LayoutInflater inflater = ((Activity)context).getLayoutInflater();
				row = inflater.inflate(layoutResourceId, parent, false);
				
				holder = new SongHolder();
				holder.name = (TextView)row.findViewById(R.id.name);
				holder.artist = (TextView)row.findViewById(R.id.artist);
				holder.submitter = (TextView)row.findViewById(R.id.submitter);
				holder.emblem = (ImageView)row.findViewById(R.id.emblem);
				
				row.setTag(holder);
			}
			else
			{
				holder = (SongHolder)row.getTag();
			}

			Song song = songs.get(position);

			holder.name.setText(song.getName());
			holder.artist.setText(song.getArtist());
			holder.submitter.setText(song.getSubmitter());
			
			if (song.isPlaying())
			{
				holder.emblem.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.emblem.setVisibility(View.INVISIBLE);
			}
			
			return row;
		}
		
		private static class SongHolder
		{
			public TextView name;
			public TextView artist;
			public TextView submitter;
			public ImageView emblem;
		}
	}
}